package com.tb.controller;

import com.tb.dubbo.IUserService;
import com.tb.pojo.User;
import com.tb.utils.MD5;
import com.tb.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IUserService iUserService;

    /**
     * @api {post} /login/checkUser 前台用户登录   王玺玉
     * @apiDescription 用户的登录验证
     * @apiGroup User API
     *
     * @apiParam {String}  username 用户名
     * @apiParam {String}  password 密码
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "username":"zhangsan",
     *          "password":"123456"
     *      }
     *
     * @apiVersion 1.0.0
     *
     *@apiSuccess  {int} code 返回状态码 0:正常 1:错误
     *@apiSuccess  {string} msg 返回错误信息
     *@apiSuccess  {Object} data 返回的成功信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "登录成功",
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "参数不合法",
     *     "data": null
     * }
     *
     * @apiSampleRequest /login/checkUser?username=zhangsan&password=123456
     */
    @PostMapping("/checkUser")
    public Msg checkUser(@RequestParam (value = "username",defaultValue = "")String username,
                         @RequestParam(value = "password",defaultValue = "")String password,
                         HttpSession httpSession
                        ){

        if ("".equals(username)||"".equals(password)){
            return Msg.error(1,"请输入信息");
        }else {
            User user=new User();
            user.setUserName(username);
            MD5 md5=new MD5();
            password=md5.getMD5ofStr(password);
            user.setUserPass(password);
            User checkUser = iUserService.checkOne(user);
            if (checkUser==null){
                return Msg.error(1,"用户名或密码错误");
            }else {
                int status=checkUser.getUserStatus();
                if (0>=status||1<status){
                    return Msg.error(1,"账户被冻结");
                }else {
                    if (username.equals(checkUser.getUserName())&&password.equals(checkUser.getUserPass())){
                        String res="登录成功";
                        httpSession.setAttribute("User",checkUser);
                        return Msg.success(res);
                    }else {
                        String res="账号或者密码有误,请重新输入";
                        return Msg.error(1,res);
                    }
                }

            }
        }
    }

    /**
     * @api {post} /login/checkAdmin 后台管理员登录   王玺玉
     * @apiDescription 管理员用户的登录验证
     * @apiGroup User API
     *
     * @apiParam {String}  username=123 用户名
     * @apiParam {String}  password 密码
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "username":"zhangsan",
     *          "password":"123456"
     *      }
     *
     * @apiVersion 1.0.0
     *
     *@apiSuccess  {int} code 返回状态码 0:正常 1:错误
     *@apiSuccess  {string} msg 返回错误信息
     *@apiSuccess  {Object} data 返回的成功信息
     *@apiSuccess  (data) {string} res  登录成功
     *@apiSuccess  (data) {string} rolerId  用户角色  0：超管  1||2：普通管理员  3：普通用户
     *@apiSuccess  (data) {string} userName  用户名
     *
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *   "code": 0,
     *   "msg": "",
     *   "data": {
     *     "res": "登录成功",
     *     "rolerId": 0,
     *     "userName": "root"
     *   }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "参数不合法",
     *     "data": null
     * }
     *
     * @apiSampleRequest /login/checkAdmin?username=lisi&password=123456
     */
    @PostMapping("/checkAdmin")
    public Msg checkAdmin(@RequestParam (value = "username",defaultValue = "")String username,
                         @RequestParam(value = "password",defaultValue = "")String password,
                          HttpSession httpSession
                          ){
        if ("".equals(username)||"".equals(password)){
            return Msg.error(1,"请输入信息");
        }else {
            User user=new User();
            user.setUserName(username);
            MD5 md5=new MD5();
            password=md5.getMD5ofStr(password);
            user.setUserPass(password);
            User checkUser = iUserService.checkAdmin(user);
            if (checkUser==null){
                return Msg.error(1,"用户名或密码错误");
            }else {
                int rolerId=checkUser.getRolerId();
                //超级管理员
                if (0==rolerId){
                    if (username.equals(checkUser.getUserName())&&password.equals(checkUser.getUserPass())){
                        String res="登录成功";
                        Map<String ,Object> map=new HashMap<>();
                        map.put("rolerId",rolerId);
                        map.put("userName",username);
                        map.put("res",res);
                        httpSession.setAttribute("AdminUser",checkUser);
                        return Msg.success(map);
                    }else {
                        String res="账号或者密码有误,请重新输入";
                        return Msg.error(1,res);
                    }
                }else {
                    int status=checkUser.getUserStatus();
                    if (0>=status||1<status){
                        return Msg.error(1,"账户被冻结");
                    }else {

                        if (username.equals(checkUser.getUserName())&&password.equals(checkUser.getUserPass())){
                            String res="登录成功";
                            httpSession.setAttribute("AdminUser",checkUser);
                            Map<String ,Object> map=new HashMap<>();
                            map.put("rolerId",rolerId);
                            map.put("userName",username);
                            map.put("res",res);
                            return Msg.success(map);
                        }else {
                            String res="账号或者密码有误,请重新输入";
                            return Msg.error(1,res);
                        }
                    }
                }
            }
        }
    }

    /**
     * @api {get} /login/userLoginOut 前台用户退出登录    王玺玉
     * @apiDescription 用户退出登录
     * @apiGroup User API
     *
     *
     * @apiVersion 1.0.0
     *
     *@apiSuccess  {int} code 返回状态码 0:正常
     *@apiSuccess  {Object} data 返回的成功信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "退出成功",
     *    }
     *
     *
     * @apiSampleRequest /login/userLoginOut
     */
    @GetMapping("/userLoginOut")
    public Msg userLoginOut(HttpSession httpSession){
        httpSession.removeAttribute("User");
        return Msg.success("退出成功");
    }

    /**
     * @api {get} /login/adminLoginOut 管理员退出登录    王玺玉
     * @apiDescription 管理员退出登录
     * @apiGroup User API
     *
     *
     * @apiVersion 1.0.0
     *
     *@apiSuccess  {int} code 返回状态码 0:正常
     *@apiSuccess  {Object} data 返回的成功信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "退出成功",
     *    }
     *
     *
     * @apiSampleRequest /login/adminLoginOut
     */
    @GetMapping("/adminLoginOut")
    public Msg adminLoginOut(HttpSession httpSession){
        httpSession.removeAttribute("AdminUser");
        return Msg.success("退出成功");
    }

}
